import { request } from '@/utils/request'

// 组织接口
export interface Organization {
  id: string
  name: string
  description?: string
  type: 'company' | 'department' | 'team' | 'project_group'
  parent_id?: string
  parent_name?: string
  level: number
  path: string
  status: 'active' | 'inactive'
  manager_id?: string
  manager_name?: string
  member_count: number
  project_count: number
  created_at: string
  updated_at: string
}

// 组织查询参数
export interface OrganizationQueryParams {
  page?: number
  size?: number
  search?: string
  type?: string
  parent_id?: string
  status?: string
  manager_id?: string
  sort_by?: string
  sort_order?: 'asc' | 'desc'
}

// 组织列表响应
export interface OrganizationListResponse {
  items: Organization[]
  total: number
  page: number
  size: number
  total_pages: number
}

// 创建组织参数
export interface CreateOrganizationParams {
  name: string
  description?: string
  type: 'company' | 'department' | 'team' | 'project_group'
  parent_id?: string
  manager_id?: string
}

// 更新组织参数
export interface UpdateOrganizationParams {
  name?: string
  description?: string
  type?: 'company' | 'department' | 'team' | 'project_group'
  parent_id?: string
  manager_id?: string
  status?: 'active' | 'inactive'
}

// 获取组织列表
export const getOrganizationList = (params?: OrganizationQueryParams) => {
  return request.get<OrganizationListResponse>('/organizations', {
    params
  })
}

// 获取组织树形结构
export const getOrganizationTree = () => {
  return request.get<Organization[]>('/organizations/tree')
}

// 获取组织详情
export const getOrganizationDetail = (id: string) => {
  return request.get<Organization>(`/organizations/${id}`)
}

// 创建组织
export const createOrganization = (data: CreateOrganizationParams) => {
  return request.post<Organization>('/organizations', data)
}

// 更新组织
export const updateOrganization = (id: string, data: UpdateOrganizationParams) => {
  return request.put<Organization>(`/organizations/${id}`, data)
}

// 删除组织
export const deleteOrganization = (id: string) => {
  return request.delete(`/organizations/${id}`)
}

// 移动组织
export const moveOrganization = (id: string, parentId?: string) => {
  return request.patch(`/organizations/${id}/move`, { parent_id: parentId })
}

// 组织成员管理
export interface OrganizationMember {
  user_id: string
  username: string
  name: string
  email: string
  avatar?: string
  role: string
  position?: string
  joined_at: string
}

// 获取组织成员
export const getOrganizationMembers = (id: string, params?: {
  page?: number
  size?: number
  search?: string
  role?: string
}) => {
  return request.get<{
    items: OrganizationMember[]
    total: number
    page: number
    size: number
  }>(`/organizations/${id}/members`, {
    params
  })
}

// 添加组织成员
export const addOrganizationMember = (id: string, userId: string, role?: string, position?: string) => {
  return request.post(`/organizations/${id}/members`, { 
    user_id: userId, 
    role, 
    position 
  })
}

// 移除组织成员
export const removeOrganizationMember = (id: string, userId: string) => {
  return request.delete(`/organizations/${id}/members/${userId}`)
}

// 更新成员角色
export const updateMemberRole = (id: string, userId: string, role: string, position?: string) => {
  return request.patch(`/organizations/${id}/members/${userId}`, { role, position })
}

// 批量添加成员
export const batchAddMembers = (id: string, members: Array<{
  user_id: string
  role?: string
  position?: string
}>) => {
  return request.post(`/organizations/${id}/members/batch`, { members })
}

// 批量移除成员
export const batchRemoveMembers = (id: string, userIds: string[]) => {
  return request.post(`/organizations/${id}/members/batch-remove`, { user_ids: userIds })
}

// 获取组织项目
export const getOrganizationProjects = (id: string, params?: {
  page?: number
  page_size?: number
  search?: string
  status?: string
  priority?: string
}) => {
  return request.get(`/organizations/${id}/projects`, {
    params
  })
}

// 获取组织统计
export interface OrganizationStats {
  total_organizations: number
  active_organizations: number
  inactive_organizations: number
  total_members: number
  total_projects: number
  organization_distribution: Array<{
    type: string
    count: number
  }>
  member_distribution: Array<{
    organization_id: string
    organization_name: string
    member_count: number
  }>
}

export const getOrganizationStats = () => {
  return request.get<OrganizationStats>('/organizations/stats')
}

// 角色管理
export interface Role {
  id: string
  name: string
  description?: string
  permissions: string[]
  organization_id?: string
  is_system: boolean
  status: 'active' | 'inactive'
  created_at: string
  updated_at: string
}

// 获取角色列表
export const getRoleList = (params?: {
  page?: number
  page_size?: number
  search?: string
  status?: string
  organization_id?: string
  is_system?: boolean
}) => {
  return request.get<{
    items: Role[]
    total: number
    page: number
    page_size: number
  }>('/roles', {
    params
  })
}

// 获取角色详情
export const getRoleDetail = (id: string) => {
  return request.get<Role>(`/roles/${id}`)
}

// 创建角色
export const createRole = (data: {
  name: string
  description?: string
  permissions: string[]
  organization_id?: string
}) => {
  return request.post<Role>('/roles', data)
}

// 更新角色
export const updateRole = (id: string, data: {
  name?: string
  description?: string
  permissions?: string[]
  status?: 'active' | 'inactive'
}) => {
  return request.put<Role>(`/roles/${id}`, data)
}

// 删除角色
export const deleteRole = (id: string) => {
  return request.delete(`/roles/${id}`)
}

// 获取所有权限
export interface Permission {
  id: string
  name: string
  description?: string
  resource: string
  action: string
  category: string
}

export const getPermissionList = () => {
  return request.get<Permission[]>('/permissions')
}

// 获取权限分组
export const getPermissionGroups = () => {
  return request.get<Record<string, Permission[]>>('/permissions/groups')
}

// 检查用户权限
export const checkUserPermission = (userId: string, permission: string, resourceId?: string) => {
  return request.get<{ has_permission: boolean }>('/permissions/check', {
    params: { user_id: userId, permission, resource_id: resourceId }
  })
}

// 获取用户权限列表
export const getUserPermissions = (userId: string, organizationId?: string) => {
  return request.get<string[]>(`/users/${userId}/permissions`, {
    params: organizationId ? { organization_id: organizationId } : undefined
  })
}